package com.ruoyi.web.controller.system;

import com.google.common.collect.Maps;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.util.ShiroUtils;
import com.ruoyi.system.domain.Appendix;
import com.ruoyi.system.domain.Writings;
import com.ruoyi.system.enums.AuditStateEnum;
import com.ruoyi.system.enums.DocTypeEnum;
import com.ruoyi.system.service.IAppendixService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.IWritingsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 著作Controller
 *
 * @author ruoyi
 * @date 2020-04-12
 */
@Controller
@RequestMapping("/system/writings")
public class WritingsController extends BaseController {
    private String prefix = "system/writings";

    @Autowired
    private IWritingsService writingsService;
    @Autowired
    private ISysDeptService sysDeptService;
    @Autowired
    private IAppendixService appendixService;

    @RequiresPermissions("system:writings:view")
    @GetMapping()
    public String writings() {
        return prefix + "/writings";
    }

    /**
     * 查询著作列表
     */
    @RequiresPermissions("system:writings:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Writings writings) {
        startPage();
        List<Writings> list = writingsService.selectWritingsList(writings);
        return getDataTable(list);
    }

    /**
     * 导出著作列表
     */
    @RequiresPermissions("system:writings:export")
    @Log(title = "著作", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(Writings writings) {
        List<Writings> list = writingsService.selectWritingsList(writings);
        ExcelUtil<Writings> util = new ExcelUtil<Writings>(Writings.class);
        return util.exportExcel(list, "writings");
    }

    /**
     * 新增著作
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存著作
     */
    @RequiresPermissions("system:writings:add")
    @Log(title = "著作", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(Writings writings) {
        writings.setDocType(DocTypeEnum.WRITINGS.getValue());
        writings.setCreator(ShiroUtils.getUserId());
        writings.setDepartment(sysDeptService.selectDeptById(writings.getDepartmentId()).getDeptName());
        writings.setReportUnit(sysDeptService.selectDeptById(writings.getReportUnitId()).getDeptName());
        return toAjax(writingsService.insertWritings(writings));
    }

    /**
     * 修改著作
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        Writings writings = writingsService.selectWritingsById(id);
        mmap.put("writings", writings);
        mmap.put("dept",sysDeptService.selectDept(writings.getReportUnitId()));
        return prefix + "/edit";
    }

    /**
     * 修改保存著作
     */
    @RequiresPermissions("system:writings:edit")
    @Log(title = "著作", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(Writings writings) {
        writings.setDepartment(sysDeptService.selectDeptById(writings.getDepartmentId()).getDeptName());
        writings.setReportUnit(sysDeptService.selectDeptById(writings.getReportUnitId()).getDeptName());
        return toAjax(writingsService.updateWritings(writings));
    }

    /**
     * 删除著作
     */
    @RequiresPermissions("system:writings:remove")
    @Log(title = "著作", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(writingsService.deleteWritingsByIds(ids));
    }

    /**
     * 提交审核
     */
    @RequiresPermissions("system:writings:applyAudit")
    @Log(title = "著作", businessType = BusinessType.UPDATE)
    @PostMapping("/applyAudit")
    @ResponseBody
    public AjaxResult applyAudit(long id) {
        Writings writings = writingsService.selectWritingsById(id);
        if (AuditStateEnum.NOT_AUDIT.getValue() != writings.getAuditState().intValue()) {
            return error("请不要重复发起审核");
        } else {
            writingsService.applyAutdit(writings,ShiroUtils.getSysUser());
            return success();
        }

    }


    /**
     * 修改著作
     */
    @GetMapping("/detail/{id}")
    @RequiresPermissions("system:writings:detail")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {
        Writings writings = writingsService.selectWritingsById(id);
        mmap.put("doc", writings);
        List<Appendix> appendices = appendixService.listByDocTypeAndId(writings.getId(), writings.getDocType());
        mmap.put("appendices",appendices);
        return prefix + "/detail";
    }
}
